package com.ludomancy.starwave;

public class Level implements ShipTypes
{
	public String description;
	private String[][] map;
	
	public Level(String[][] map, String description)
	{
		this.map = map;
		this.description = description;
	}
	
	public static Level setupMission(Player player, AIPlayer owner, int level)
	{
		Level newMission = (Level) LevelManager.levels.elementAt(level);
		String[][] map = newMission.map;
		
		// load level data
		int[][] playerField = new int[Game.GRID_HEIGHT][Game.ACTIVE_AREA_LENGTH];
		int[][] aiField 	= new int[Game.GRID_HEIGHT][Game.GRID_WIDTH - Game.ACTIVE_AREA_LENGTH - 1];
		for (int i = 0; i < map.length; i++)
		{
			for (int j = 0; j < map[0].length; j++)
			{
				String shipID = map[i][j];
				if(j < Game.ACTIVE_AREA_LENGTH)
				{
					if(Game.EDITOR)
					{
						playerField[i][j] = NON;
					}
					else
					{
						playerField[i][j] = Ship.stringToType(shipID);
					}
				}
				else
				if(j > Game.ACTIVE_AREA_LENGTH)
				{
					if(Game.EDITOR)
					{
						aiField[i][j - Game.ACTIVE_AREA_LENGTH - 1] = NON;
					}
					else
					{
						aiField[i][j - Game.ACTIVE_AREA_LENGTH - 1] = Ship.stringToType(shipID);
					}
				}
			}
		}
				
		// init player field
		player.initialSetup = playerField;
		
		// init ai field
		for(int y = 0 ; y != aiField.length ; y++)
		{
			for(int x = 0 ; x != aiField[0].length ; x++)
			{
				Ship s = Ship.createPrototype(aiField[y][x], owner, Game.GRID_WIDTH - Game.ACTIVE_AREA_LENGTH + x, y);
				if(s != null)
				{
					Game.game.addShip(s);
				}
			}
		}
		return newMission;
	}	
}
